import api from "./api.js"
export default {
	login_user() {
		let UserInfo = uni.getStorageSync('user_info') //用户信息
		if (!UserInfo) {
			uni.getUserProfile({
				desc: 'Wexin', // 这个参数是必须的
				success: res => {
					uni.login({
						provider: 'weixin',
						success: res1 => {
							uni.setStorageSync('code', res1.code)
							let obj = {
								nickname: res.userInfo.nickName,
								avatar: res.userInfo.avatarUrl,
								code: res1.code
							}
							api.wx_login(obj).then(login => {
								console.log(login)
								uni.hideLoading();
								//请求成功后的判断
								res = {
									...res,
									...login.data
								}
								uni.setStorage({
									key: 'user_info',
									data: res,
									success: function() {
										uni.showToast({
											title: '用户授权成功',
											icon: 'none'
										})
										// 获取当前页面的路由几参数
										function get_fns(data) {
											var arr = Object.keys(data);
											var str = "?";
											for (var i in data) {
												str += i;
												str += "=";
												str += data[i];
												str += "&";
											}
											return str.substr(0, str
												.length - 1)
										}
										let routes =
											getCurrentPages(); // 获取当前打开过的页面路由数组
										const filterRouters = routes.reduce((acc, cur) => {
										  const isHave = acc.find(item => item.route == cur.route);
										  if (!isHave) {
											acc.push(cur);
										  }
										  return acc;
										}, []);//路由数组去重，如果有两个登录页面只保留一个
										
										let curRoute = filterRouters[filterRouters
												.length - 2]
											.route //获取当前页面路由
										let curParam = filterRouters[filterRouters
											.length - 2].options; //获取路由参数
										// 拼接参数
										let param =
											`/${curRoute}${get_fns(curParam)}`
										console.log(curRoute, 147258369)
										api.user_info().then(users => {
											res.id = users.data.data.id
											uni.setStorage({
												key: 'user_info',
												data: res,
												success() {
													// if (!uni.getStorageSync('store_dz').id) return api.store_saveUsetStore({store_id: uni.getStorageSync('store_dz').id}).then(res => {
													// 			console.log(res)
													// 	})
													if(uni.getStorageSync('store_dz')){
														api.store_saveUsetStore({ store_id: uni.getStorageSync('store_dz').id }).then(data => {
															console.log(data, '绑定店铺')
														})
													}
												}
											})
											uni.setStorageSync('focus_on', users.data.data.focus_on)
											if (uni.getStorageSync('scene_id')) {
												let scene_id = uni.getStorageSync('scene_id')
												console.log(scene_id,'这是绑定用户的id')
												api.User_savePid({
													id: scene_id
												}).then(res => {
													console.log('绑定成功',res)
												})
											}
										})

										uni.reLaunch({ //关闭其他页面跳转到该页面
											url: param
										});
										uni.setStorage({
											key: 'isLoginPage',
											data: false,
										})
									}
								});
							})
						}
					})
				},
			})
		}
	},
	emits(name, el) {
		let ispages = uni.getStorageSync('isTabBarOrder') //判断预约页面是否打开
		if (ispages) { //打开过
			uni.$emit(name, el)
		} else { //没打开
			uni.switchTab({
				url: '/pages/tabBar/order/order'
			})
			setTimeout(() => {
				uni.$emit(name, el)
			}, 1000)
		}
	}
}
